#include "FKNNRecordLayer.h"

bool FKNNRecordLayer::init()
{
	if (!Layer::init())
		return false;

// 	LayerColor* layerColor = LayerColor::create(Color4B(0, 0, 0, 180));
// 	addChild(layerColor);
// 	layerColor->setContentSize(Director::getInstance()->getVisibleSize());

	layoutMain = CSLoader::createNode("recordLayer.csb");
	addChild(layoutMain);

	auto btn = (Button*)layoutMain->getChildByName("Button_close");
	btn->addTouchEventListener(CC_CALLBACK_2(FKNNRecordLayer::BtnCallBack, this));
	return true;
}

void FKNNRecordLayer::onEnter()
{
	Layer::onEnter();
	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(FKNNRecordLayer::onTouchBegan, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	_touchListener = listener;

	Layout* panelItem = (Layout*)layoutMain->getChildByName("Panel_item");
	ListView* listView = (ListView*)layoutMain->getChildByName("ListView_record");
	for (int i = 0; i < 10; ++i)
	{
		Layout* newItem = (Layout*)panelItem->clone();
		listView->pushBackCustomItem(newItem);
	}
}

void FKNNRecordLayer::onExit()
{
	_eventDispatcher->removeEventListener(_touchListener);
	_touchListener = nullptr;
	Layer::onExit();
}

bool FKNNRecordLayer::onTouchBegan( Touch *pTouch, Event *pEvent )
{
// 	if (!layoutMain->getChildByName("Panel_chat")->getBoundingBox().containsPoint(pTouch->getLocation()))
// 	{
// 		this->runAction(RemoveSelf::create());
// 	}
	return true;
}

void FKNNRecordLayer::BtnCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;

	switch (touchtype)
	{
		case cocos2d::ui::Widget::TouchEventType::ENDED:
		{
			if (btn->getName().compare("Button_close") == 0)
			{
				this->runAction(RemoveSelf::create());
			}
		}
	}
}